import dt from '@dt/dt'
import ruleOpt from '@/biz/option/rule.js'
import toolMgr from '@/biz/base/tool.js'

let _this = null


function clearNoNum(val) {
  return String(val).replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$')
    .replace(/\./g,
      '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
}


function floorPic(info) {
  let api = toolMgr.unloadFile()
  let files = []
  if (info.houseTypeAnnex && info.houseTypeAnnex.length > 0) {
    info.houseTypeAnnex.forEach(item => {
      if (item) {
        files.push({
          name: item.name,
          url: item.url,
          cover: item.cover
        })
      }
    })
  }
  return {
    title: '户型图',
    key: 'houseTypeAnnex',
    uploadImg: {
      title: '户型图',
      api,
      files,
      multiple: false,
      hasCover: true,
      trueRadioTxt: '封面图',
      falseRadioTxt: '设置封面图',
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      },
      show: false,
      hideDel: _this.is_view ? true : false,
      maxlength: _this.is_view ? -1 : 30,
      format: ['jpg', 'jpeg', 'png'],
      formatErr: () => {
        _this.$Message.error('只支持上传jpg、jpeg、png图片！')
      },
    },
    data: {
      to: (v) => {
        let houseTypeAnnex = v.map(item => {
          return {
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url,
            cover: item.cover ? item.cover : '2'
          }
        })
        return {
          houseTypeAnnex
        }
      }
    }
  }
}

function options(vm) {
  _this = vm
  let arr = [
    {
      title: '户型名称',
      placeholder: '请输入户型名称',
      key: 'houseTypeName',
      disabled: _this.is_view,
      col: 12,
      input: { maxlength: 20 },
      rules: [{
        required: true,
        message: '户型名称不能为空',
        trigger: 'blur'
      }]
    },
    {
      title: '楼层',
      key: 'floor',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: dt.dictInfo().floor,
      },
    },
    {
      title: '装修',
      key: 'decoration',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: dt.dictInfo().residenceDecoration,
      },
    },
    {
      title: '朝向',
      key: 'orientation',
      col: 12,
      disabled: _this.is_view,
      select: {
        options: dt.dictInfo().orientation,
      },
    },
    {
      title: '是否可分割',
      key: 'partitionIf',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: [{ key: '1', value: '可分割' }, { key: '2', value: '不可分割' }],
      },
    },
    {
      title: '层高',
      key: 'storyHeightStart',
      disabled: _this.is_view,
      col: 12,
      input: {
        appendTxt: 'm',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '建面',
      key: 'floorAreaStart',
      col: 12,
      disabled: _this.is_view,
      input: {
        appendTxt: '㎡',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '单价',
      key: 'unitPriceStart',
      col: 12,
      disabled: _this.is_view,
      input: {
        appendTxt: '元/㎡',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '总价',
      key: 'totalPriceStart',
      col: 12,
      disabled: _this.is_view,
      input: {
        appendTxt: '万元',
        blur: (val, option) => {
          _this.info[option.key] = clearNoNum(val)
        }
      }
    },
    {
      title: '销售状态',
      key: 'salesStatus',
      col: 12,
      disabled: _this.is_view,
      radio: {
        options: dt.dictInfo().buSalesStatus,
      },
      rules: [{
        required: true,
        message: '销售状态不能为空',
        trigger: 'blur'
      }]
    },
    floorPic(_this.info),
    {
      title: '户型点评',
      key: 'houseTypeComment',
      col: 20,
      disabled: _this.is_view,
      input: {
        type: 'textarea',
        maxlength: 200,
        autosize: {
          minRows: 2,
          maxRows: 5
        }
      },
    }
  ]
  return arr
}

export default {
  options
}